/*
 * Copyright (c) 2019 Wormpex.com. All Rights Reserved.
 */

package com.sk.threndpool;

import org.jetbrains.annotations.NotNull;

import java.util.Comparator;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ke.su date: 2019-11-04 Time: 下午7:51
 */
public class Test1 {


    public static void main(String[] args) {


        // ExecutorService executorService1 = Executors.newCachedThreadPool();

        //
        // ThreadPoolExecutor arrayExecutorService =
        //         new ThreadPoolExecutor(10, 20, 300, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1), new MyThreadFactory(), new MyAbortPolicy());


        // ThreadPoolExecutor executorService =
        //         new ThreadPoolExecutor(10, 20, 300, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), new MyThreadFactory(), new MyAbortPolicy());


        ThreadPoolExecutor executorService =
                new ThreadPoolExecutor(10, 20, 300, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>(100), new MyThreadFactory(), new MyAbortPolicy());


        // ThreadPoolExecutor executorService =
        //         new ThreadPoolExecutor(10, 20, 300, TimeUnit.SECONDS, new SynchronousQueue(), new MyThreadFactory(), new MyAbortPolicy());

        for (long i = 0; i < 10000000000000L; i++) {
            executorService.execute(new The(i));

            if(i%10000==0){
                System.out.println("ssss="+i);
            }

        }


        executorService.shutdown();

        // ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(100);
        //
         LinkedBlockingQueue blockingQueue = new LinkedBlockingQueue<>(1);
        //
        // PriorityBlockingQueue priorityBlockingQueue = new PriorityBlockingQueue(1);
        //
        // DelayQueue delayQueue = new DelayQueue();
        //
        // SynchronousQueue synchronousQueue = new SynchronousQueue();

        LinkedTransferQueue linkedTransferQueue = new LinkedTransferQueue();

    }
}

class The implements Runnable ,Comparable<The>{
    public static AtomicInteger atomicInteger = new AtomicInteger(1);

    public long i;

    public The(long i) {
        this.i = i;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int s = atomicInteger.getAndUpdate(p -> p + 1);
        System.out.println(s);
    }

    @Override
    public int compareTo(@NotNull The o) {
        return 0;
    }
}

class MyThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(@NotNull Runnable r) {
        return new Thread(r);
    }
}

class MyAbortPolicy implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        throw new RejectedExecutionException("daadadaw");
    }
}

